// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.areas.isoregion.regions;

import java.util.ArrayList;
import zombie.core.Color;
import zombie.core.Core;
import zombie.iso.areas.isoregion.IsoRegions;

public final class IsoWorldRegion implements IWorldRegion {
    private final IsoRegionManager manager;
    private boolean isInPool = false;
    private int ID;
    private Color color;
    private boolean enclosed = true;
    private ArrayList<IsoChunkRegion> isoChunkRegions = new ArrayList();
    private int squareSize = 0;
    private int roofCnt = 0;
    private boolean isDirtyEnclosed = false;
    private boolean isDirtyRoofed = false;
    private ArrayList<IsoWorldRegion> neighbors = new ArrayList();

    public int getID() {
        return this.ID;
    }

    public Color getColor() {
        return this.color;
    }

    public int size() {
        return this.isoChunkRegions.size();
    }

    @Override
    public int getSquareSize() {
        return this.squareSize;
    }

    protected boolean isInPool() {
        return this.isInPool;
    }

    protected IsoWorldRegion(IsoRegionManager isoRegionManager) {
        this.manager = isoRegionManager;
    }

    protected void init(int _int) {
        this.isInPool = false;
        this.ID = _int;
        if (this.color == null) {
            this.color = this.manager.getColor();
        }

        this.squareSize = 0;
        this.roofCnt = 0;
        this.enclosed = true;
        this.isDirtyEnclosed = false;
        this.isDirtyRoofed = false;
    }

    protected IsoWorldRegion reset() {
        this.isInPool = true;
        this.ID = -1;
        this.squareSize = 0;
        this.roofCnt = 0;
        this.enclosed = true;
        this.isDirtyRoofed = false;
        this.isDirtyEnclosed = false;
        this.unlinkNeighbors();
        if (this.isoChunkRegions.size() > 0) {
            if (Core.bDebug) {
                throw new RuntimeException("MasterRegion.reset Resetting master region which still has chunk regions");
            }

            IsoRegions.warn("MasterRegion.reset Resetting master region which still has chunk regions");

            for (int _int = 0; _int < this.isoChunkRegions.size(); _int++) {
                IsoChunkRegion isoChunkRegion = (IsoChunkRegion)this.isoChunkRegions.get(_int);
                isoChunkRegion.setIsoWorldRegion(null);
            }

            this.isoChunkRegions.clear();
        }

        return this;
    }

    public void unlinkNeighbors() {
        for (int _int = 0; _int < this.neighbors.size(); _int++) {
            IsoWorldRegion isoWorldRegion1 = (IsoWorldRegion)this.neighbors.get(_int);
            isoWorldRegion1.removeNeighbor(this);
        }

        this.neighbors.clear();
    }

    public void linkNeighbors() {
        for (int int0 = 0; int0 < this.isoChunkRegions.size(); int0++) {
            IsoChunkRegion isoChunkRegion0 = (IsoChunkRegion)this.isoChunkRegions.get(int0);

            for (int int1 = 0; int1 < isoChunkRegion0.getAllNeighbors().size(); int1++) {
                IsoChunkRegion isoChunkRegion1 = (IsoChunkRegion)isoChunkRegion0.getAllNeighbors().get(int1);
                if (isoChunkRegion1.getIsoWorldRegion() != null && isoChunkRegion1.getIsoWorldRegion() != this) {
                    this.addNeighbor(isoChunkRegion1.getIsoWorldRegion());
                    isoChunkRegion1.getIsoWorldRegion().addNeighbor(this);
                }
            }
        }
    }

    private void addNeighbor(IsoWorldRegion isoWorldRegion0) {
        if (!this.neighbors.contains(isoWorldRegion0)) {
            this.neighbors.add(isoWorldRegion0);
        }
    }

    private void removeNeighbor(IsoWorldRegion isoWorldRegion0) {
        this.neighbors.remove(isoWorldRegion0);
    }

    @Override
    public ArrayList<IsoWorldRegion> getNeighbors() {
        return this.neighbors;
    }

    @Override
    public ArrayList<IsoWorldRegion> getDebugConnectedNeighborCopy() {
        ArrayList arrayList = new ArrayList();
        if (this.neighbors.size() == 0) {
            return arrayList;
        } else {
            arrayList.addAll(this.neighbors);
            return arrayList;
        }
    }

    @Override
    public boolean isFogMask() {
        return this.isEnclosed() && this.isFullyRoofed();
    }

    @Override
    public boolean isPlayerRoom() {
        return this.isFogMask();
    }

    @Override
    public boolean isFullyRoofed() {
        return this.roofCnt == this.squareSize;
    }

    public float getRoofedPercentage() {
        return this.squareSize == 0 ? 0.0F : (float)this.roofCnt / (float)this.squareSize;
    }

    @Override
    public int getRoofCnt() {
        return this.roofCnt;
    }

    protected void addRoof() {
        this.roofCnt++;
        if (this.roofCnt > this.squareSize) {
            IsoRegions.warn("WorldRegion.addRoof roofCount exceed squareSize.");
            this.roofCnt = this.squareSize;
        }
    }

    protected void removeRoofs(int _int) {
        if (_int > 0) {
            this.roofCnt -= _int;
            if (this.roofCnt < 0) {
                IsoRegions.warn("MasterRegion.removeRoofs Roofcount managed to get below zero.");
                this.roofCnt = 0;
            }
        }
    }

    public void addIsoChunkRegion(IsoChunkRegion region) {
        if (!this.isoChunkRegions.contains(region)) {
            this.squareSize = this.squareSize + region.getSquareSize();
            this.roofCnt = this.roofCnt + region.getRoofCnt();
            this.isDirtyEnclosed = true;
            this.isoChunkRegions.add(region);
            region.setIsoWorldRegion(this);
        }
    }

    protected void removeIsoChunkRegion(IsoChunkRegion isoChunkRegion) {
        if (this.isoChunkRegions.remove(isoChunkRegion)) {
            this.squareSize = this.squareSize - isoChunkRegion.getSquareSize();
            this.roofCnt = this.roofCnt - isoChunkRegion.getRoofCnt();
            this.isDirtyEnclosed = true;
            isoChunkRegion.setIsoWorldRegion(null);
        }
    }

    public boolean containsIsoChunkRegion(IsoChunkRegion region) {
        return this.isoChunkRegions.contains(region);
    }

    public ArrayList<IsoChunkRegion> swapIsoChunkRegions(ArrayList<IsoChunkRegion> newlist) {
        ArrayList arrayList = this.isoChunkRegions;
        this.isoChunkRegions = newlist;
        return arrayList;
    }

    protected void resetSquareSize() {
        this.squareSize = 0;
    }

    protected void setDirtyEnclosed() {
        this.isDirtyEnclosed = true;
    }

    public boolean isEnclosed() {
        if (this.isDirtyEnclosed) {
            this.recalcEnclosed();
        }

        return this.enclosed;
    }

    private void recalcEnclosed() {
        this.isDirtyEnclosed = false;
        this.enclosed = true;

        for (int _int = 0; _int < this.isoChunkRegions.size(); _int++) {
            IsoChunkRegion isoChunkRegion = (IsoChunkRegion)this.isoChunkRegions.get(_int);
            if (!isoChunkRegion.getIsEnclosed()) {
                this.enclosed = false;
            }
        }
    }

    public void merge(IsoWorldRegion other) {
        if (other.isoChunkRegions.size() > 0) {
            for (int int0 = other.isoChunkRegions.size() - 1; int0 >= 0; int0--) {
                IsoChunkRegion isoChunkRegion = (IsoChunkRegion)other.isoChunkRegions.get(int0);
                other.removeIsoChunkRegion(isoChunkRegion);
                this.addIsoChunkRegion(isoChunkRegion);
            }

            this.isDirtyEnclosed = true;
            other.isoChunkRegions.clear();
        }

        if (other.neighbors.size() > 0) {
            for (int int1 = 0; int1 < other.neighbors.size(); int1++) {
                IsoWorldRegion isoWorldRegion = (IsoWorldRegion)other.neighbors.get(int1);
                isoWorldRegion.removeNeighbor(other);
                this.addNeighbor(isoWorldRegion);
            }

            other.neighbors.clear();
        }

        this.manager.releaseIsoWorldRegion(other);
    }

    @Override
    public ArrayList<IsoChunkRegion> getDebugIsoChunkRegionCopy() {
        ArrayList arrayList = new ArrayList();
        arrayList.addAll(this.isoChunkRegions);
        return arrayList;
    }
}
